package com.zzy.springbootdemo.design_01_factory.spring.autowire;

import com.zzy.springbootdemo.design_01_factory.spring.autowire.annotation.Strategy;
import com.zzy.springbootdemo.design_01_factory.spring.common.Constants;
import com.zzy.springbootdemo.design_01_factory.spring.service.RewardSpringService;
import org.springframework.core.annotation.AnnotationUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RewardConfig {

    @Resource
    private List<RewardSpringService> rewardList;

    public Map<Constants.REWARD, RewardSpringService> getRewardAutoWireMap() {
        return rewardAutoWireMap;
    }

    protected static Map<Constants.REWARD,RewardSpringService> rewardAutoWireMap = new ConcurrentHashMap<>();

    @PostConstruct
    private void init(){
        rewardList.forEach(reward ->{
            Strategy strategy = AnnotationUtils.findAnnotation(reward.getClass(),Strategy.class);
            if(strategy != null){
                rewardAutoWireMap.put(strategy.rewardType(),reward);
            }
        });
    }
}
